﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class MenuDetailDAO : Repository<Model.Entities.Model_MenuDetail>, IMenuDeatilDAO
    {
        public override bool Add(Model.Entities.Model_MenuDetail entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_MenuDetail entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_MenuDetail menuDetail = GetByID(id);
            return Delete(menuDetail);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                using (System.Transactions.TransactionScope scope = Chinaer.Core.Helper.TransactionHelper.Instance.BeginTransaction())
                {
                    var queryResult = from m in context.MenuDetails
                                      where listID.Contains(m.ID) && m.IsUse == true && m.IsDelete == false
                                      select m;
                    foreach (var item in queryResult)
                    {
                        context.Entry<Model.Entities.Model_MenuDetail>(item).State = System.Data.Entity.EntityState.Modified;
                        item.IsDelete = true;
                    }
                    int result = context.SaveChanges();
                    scope.Complete();
                    return result;
                }
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) != null ? true : false;
        }
        public override Model.Entities.Model_MenuDetail GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuDetails.FirstOrDefault(m => m.ID == id && m.IsUse == true && m.IsDelete == false);
            }
        }
        public override IList<Model.Entities.Model_MenuDetail> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_MenuDetail entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }

        public List<Model.Entities.Model_MenuDetail> GetMenuDetailByMenuCategoryID(string menuCategoryID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuDetails.Where(m => m.SubCategoryID == menuCategoryID).ToList();
            }
        }


        public List<Model.Entities.Model_MenuDetail> GetMenuDetailByDianPuID(string dianPuID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuDetails.Where(m => m.DianPuID == dianPuID).ToList();
            }
        }


        public List<Model.Entities.Model_MenuDetail> GetAllMenuDetail()
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuDetails.ToList();
            }
        }
    }
}
